package losrebellos.media.cuepoint.net 
{
	import losrebellos.net.NetStatus;

	import org.openvideoplayer.net.OvpNetStream;

	import flash.events.EventDispatcher;
	import flash.events.NetStatusEvent;

	/*
	 *
	 * @author los rebellos - ben@benoitvinay.com
	 *
	 */
	public class OvpNetStreamCuePointManager extends CuePointManager implements IOvpNetStreamCuePointManager 
	{
		/*
		 *
		 * VARIABLES
		 *
		 */
		private var _ovpNetStream:OvpNetStream;
		
		
		/*
		 *
		 * CONSTRUCTOR
		 *
		 */
		public function OvpNetStreamCuePointManager(media:EventDispatcher = null, custom:Boolean = true)
		{
			super(media, custom);
		}
		
		
		/*
		 * 
		 * PROPERTIES
		 * 
		 */
		public function set ovpNetStream(value:OvpNetStream):void
		{
			media = value;
		}
		override protected function set media(value:EventDispatcher):void 
		{		
			super.media = value;
			
			_ovpNetStream = value as OvpNetStream;
			_ovpNetStream.addEventListener(NetStatusEvent.NET_STATUS, onStreamStatus);
		}
		
		
		/*
		 * 
		 * TIMING
		 * 
		 */
		override protected function isMediaReady():Boolean
		{
			return _ovpNetStream == null || _ovpNetStream.isBuffering;
		}
		override protected function getTimeFromMedia():Number
		{
			return _ovpNetStream.time;
		}
		
		
		/*
		 * 
		 * NET STATUS
		 * 
		 */
		private function onStreamStatus(e:NetStatusEvent):void 
		{
			switch (NetStatus.getStatus(e).code) 
			{
				case NetStatus.PLAY_START:
				case NetStatus.BUFFER_FULL:
				case NetStatus.UNPAUSE:
					if (_restartTimer && _enabled) 
					{
						start();
					}
					break;
				case NetStatus.BUFFER_EMPTY:
				case NetStatus.PLAY_STOP:
				case NetStatus.PAUSE:
					stop();
					break;
				case "NetStream.Seek.Notify":
					stop();
					this.reset();
					break;
			}
		}
	}
}